import { computed, ref } from 'vue';
import { useRoute } from 'vue-router';
import { ActionTypeEnum } from '@/enums/commonEnum';

/**
 * 页面操作方式
 */
export function useActionType() {
  const actionType = ref<ActionTypeEnum>(ActionTypeEnum.CHECK);
  const ifEdit = computed(() => {
    return actionType.value === ActionTypeEnum.EDIT;
  });

  const ifAdd = computed(() => {
    return actionType.value === ActionTypeEnum.ADD;
  });
  const ifCheck = computed(() => {
    return actionType.value === ActionTypeEnum.CHECK;
  });
  const setActionType = () => {
    const route = useRoute();
    const query = route.query;
    actionType.value = Number(query.actionType);
  };
  return {
    ifAdd,
    ifEdit,
    ifCheck,
    actionType,
    setActionType,
  };
}

/**
 * 返回上一页
 */
export function back() {
  window.history.go(-1);
}

/**
 * 处理地区数据展示文本
 * @param data
 * @param ifAddrFirst 是否有addr前缀
 */
export function handleAddrText(data: any, ifAddrFirst = true) {
  const {
    addrProvinceName,
    provinceName,
    addrCityName,
    cityName,
    addrCountyName,
    countyName,
    addrTownShipName,
    townShipName,
    addrDetails,
    details,
  } = data || {};
  return ifAddrFirst
    ? `${addrProvinceName || ''}${addrCityName || ''}${addrCountyName || ''}${addrTownShipName || ''}${addrDetails || ''}`
    : `${provinceName || ''}${cityName || ''}${countyName || ''}${townShipName || ''}${details || ''}`;
}
